package com.crayfish.quduo.ui.activity;

import android.support.v7.widget.Toolbar;
import android.util.Log;
import android.widget.ImageView;

import com.crayfish.quduo.R;
import com.crayfish.quduo.base.BaseMvpActivity;
import com.crayfish.quduo.model.UserBean;
import com.crayfish.quduo.presenter.LoginPresenter;
import com.crayfish.quduo.presenter.contract.LoginContract;
import com.crayfish.quduo.utils.SharedPreferencesUtil;
import com.crayfish.quduo.utils.SystemUtil;
import com.crayfish.quduo.utils.ToastUtil;

import java.util.HashMap;

import butterknife.BindView;
import butterknife.OnClick;
import cn.sharesdk.framework.Platform;
import cn.sharesdk.framework.PlatformActionListener;
import cn.sharesdk.framework.PlatformDb;
import cn.sharesdk.framework.ShareSDK;
import cn.sharesdk.sina.weibo.SinaWeibo;
import cn.sharesdk.tencent.qq.QQ;

/**
 * Created by crayfish on 2017/2/24.
 */
public class LoginActivity extends BaseMvpActivity<LoginPresenter>
        implements PlatformActionListener,LoginContract.View{

    private static final String TAG = "LoginActivity";

    @BindView(R.id.toolbar)
    Toolbar toolbar;
    @BindView(R.id.iv_sina)
    ImageView iv_sina;
    @BindView(R.id.iv_qq)
    ImageView iv_qq;
    @BindView(R.id.iv_weixin)
    ImageView iv_weixin;

    @Override
    protected int getLayoutId() {
        return R.layout.activity_login;
    }

    @Override
    protected LoginPresenter createPresenter() {
        return new LoginPresenter();
    }

    @Override
    protected void initEventAndData() {
        ShareSDK.initSDK(this);
        setSupportActionBar(toolbar);
        getSupportActionBar().setDisplayHomeAsUpEnabled(true);
    }

    @OnClick(R.id.iv_sina)
    void sinaLogin(){
        Platform plat_sina = ShareSDK.getPlatform(this, SinaWeibo.NAME);
        plat_sina.SSOSetting(true);
        authorize(plat_sina);
    }

    @OnClick(R.id.iv_qq)
    void qqLogin(){
        Platform plat_qq = ShareSDK.getPlatform(this, QQ.NAME);
        plat_qq.SSOSetting(true);
        authorize(plat_qq);
    }

    @OnClick(R.id.iv_weixin)
    void weixinLogin(){
        ToastUtil.show("暂不支持");
//        ShareSDK.initSDK(this);
//        Platform wechat = ShareSDK.getPlatform(this,Wechat.NAME);
//        wechat.SSOSetting(true);
//        if(!wechat.isClientValid()) {
//            Toast.makeText(mContext,
//                    "微信未安装,请先安装微信",
//                    Toast.LENGTH_LONG).show();
//        }
    }

    @Override
    public void successLogin(UserBean userBean) {
        hideProgress();
        ToastUtil.show("登录成功");
        finish();
    }

    @Override
    public void successRegister() {
        hideProgress();
        showProgress("登录中...");
        mPresenter.login();
    }

    @Override
    public void loginOrRegister(boolean isExist) {
        hideProgress();
        if(isExist){
            showProgress("登录中...");
            mPresenter.login();
        }else{
            showProgress("注册中...");
            mPresenter.register();
        }
    }

    @Override
    public void showError(String msg) {
        hideProgress();
        SharedPreferencesUtil.clearUser(this);
        ToastUtil.show(msg);
    }

    /**
     * 第三方登录，要功能不要数据
     */
    private void authorize(Platform platform){
        showProgress("数据加载中...");
        if(platform == null){
            return;
        }
        platform.setPlatformActionListener(this);
        //关闭SSO授权
        platform.SSOSetting(false);
        platform.showUser(null);
    }

    @Override
    public void onComplete(Platform platform, int i, HashMap<String, Object> hashMap) {
        if(i == Platform.ACTION_USER_INFOR){
            PlatformDb platformDb = platform.getDb();
            UserBean userBean = new UserBean();
            if(platformDb.getUserName().equals("")){
                userBean.setUsername(SystemUtil.getRandomName());
            }else {
                userBean.setUsername(platformDb.getUserName());
            }
            userBean.setUserId(platformDb.getUserId());
            userBean.setPortrait(platformDb.getUserIcon());
            userBean.setGender(platformDb.getUserGender());
            SharedPreferencesUtil.saveUser(this,userBean);
            mPresenter.searchUser(platformDb.getUserName());
            Log.d(TAG,platformDb.getUserName());
        }
    }

    @Override
    public void onError(Platform platform, int i, Throwable throwable) {
        ToastUtil.show(this.getString(R.string.no_net));
    }

    @Override
    public void onCancel(Platform platform, int i) {
    }

    @Override
    protected void onDestroy() {
        super.onDestroy();
        ShareSDK.stopSDK(this);
    }


}
